﻿Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', '../../js/ext/ux');
Ext.require([
    'Ext.form.Panel',
    'Ext.ux.form.MultiSelect',
    'Ext.ux.form.ItemSelector'
]);

Ext.onReady(function() {

    Ext.Ajax.request({
    url: "/Manager/Sys_UserRoles/List/" + Sys_UserRoleParentId,
        params: { limit: 1000, start: 0 },
        success: function(response, config) {
            json = Ext.JSON.decode(response.responseText);

            function createDockedItems(fieldId) {
                return [{
                    xtype: 'toolbar',
                    dock: 'bottom',
                    ui: 'footer',
                    defaults: {
                        minWidth: 75
                    },
                    items: ['->', {
                        text: '清除',
                        handler: function() {
                            var field = Ext.getCmp(fieldId);
                            if (!field.readOnly && !field.disabled) {
                                field.clearValue();
                            }
                        }
                    }, {
                        text: '重置',
                        handler: function() {
                            Ext.getCmp(fieldId).up('form').getForm().reset();
                        }
                    }, {
                        text: '保存',
                        handler: function() {
                            var form = Ext.getCmp(fieldId).up('form').getForm();
                            if (form.isValid()) {
                                var checkValue = form.getValues(true);
                                checkValue = checkValue.replace("itemselector=","");
                                Ext.Msg.alert('Submitted Values', 'The following will be sent to the server: <br />' +
                                        checkValue);
                                alert(checkValue);
                                Ext.Ajax.request({
                                url: "/Manager/Sys_UserRoles/Update/" + Sys_UserRoleParentId,
                                    params: { userroles: checkValue },
                                    success: function(response, config) {
                                        Ext.MessageBox.alert("提示!", "修改成功!");
                                    },
                                    failure: function(form, action) {
                                        Ext.MessageBox.alert("提示!", "修改失败!");
                                    }
                                });

                            }
                        }
}]
}];
                    }

                    var ds = Ext.create('Ext.data.ArrayStore', {
                        data: json.data,
                        fields: ['RoleID', 'R_RoleName'],
                        sortInfo: {
                            field: 'RoleID',
                            direction: 'ASC'
                        }
                    });
                    /*
                    * Ext.ux.form.ItemSelector Example Code
                    */
                    var isForm = Ext.widget('form', {
                        title: '用户角色管理',
                        width: 700,
                        bodyPadding: 10,
                        renderTo: 'itemselector',
                        items: [{
                            xtype: 'itemselector',
                            name: 'itemselector',
                            id: 'itemselector-field',
                            anchor: '100%',
                            fieldLabel: '通过左右箭头，选择用户所属角色添加到右边方框里',
                            imagePath: '../../ext/ux/images/',
                            store: ds,
                            displayField: 'R_RoleName',
                            valueField: 'RoleID',
                            value: json.values,
                            allowBlank: false,
                            msgTarget: 'side'
}], dockedItems: createDockedItems('itemselector-field')
                        });

                    }
                });
            });

                    